package org.shj.jvm;

/**
 * 比较难以重现Long变量的操作不是原子性的，跑了蛮长时间也未打印出任何信息
 * 
 * @author Administrator
 *
 */
public class LongTest {
	
	public static long t = 0;
	
	public static class ChangeT implements Runnable{
		private long to;
		public ChangeT(long to){
			this.to = to;
		}
		
		public void run(){
			while(true){
				LongTest.t = to;
				Thread.yield();
			}
		}
	}
	
	public static class ReadT implements Runnable{
		public void run(){
			while(true){
				long tmp = LongTest.t;
				if(tmp != 111L && tmp != -999L && tmp != 333L && tmp != -444L ){
					System.out.println(tmp);
				}
				Thread.yield();
			}
		}
	}

	public static void main(String[] args) {
		new Thread(new ChangeT(111L)).start();
		new Thread(new ChangeT(-999L)).start();
		new Thread(new ChangeT(333L)).start();
		new Thread(new ChangeT(-444L)).start();
		
		new Thread(new ReadT()).start();

	}

}
